/// @Classname : HcTag
/// @Author :
/// @Time : 2024-03-25 17:02
/// @Description : //Tag组件

import 'package:flutter/material.dart';
import 'package:hc_ui/config/hc_customer_config.dart';
import 'package:hc_ui/theme/hc_button_theme.dart';
import 'package:hc_ui/theme/hc_tag_theme.dart';
import 'package:hc_ui/utils/hc_color_util.dart';
import 'package:hc_ui/widget/button/hc_base_button.dart';

enum HcTagType {
  outlined,
  state,
  elevated,
  normal,
}

class HcTag extends StatelessWidget {
  //tag的名字
  final String? text;

  //状态的按钮
  final Widget? icon;

  //是否选择
  final bool isSelected;

  //点击的效果
  final VoidCallback? onPressed;

  //是否禁用
  final bool disabled;

  //icon和文字是否翻转
  final bool isReverse;

  //内边距
  final EdgeInsets? padding;

  final BorderRadius? borderRadius;

  //渐变色
  final Gradient? gradient;

  //Tag的默认样式
  final HcTagStyle? tagStyle;

  //Tag的主题配置
  final HcTagTheme? themeData;

  //最大显示行数
  final int? maxLines;

  //tag的显示类型
  final HcTagType tagType;

  //选择后的类型
  final HcTagType? selectedTagType;

  //文字颜色
  final Color? textColor;

  //图标颜色
  final Color? iconColor;

  //边框颜色
  final Color? borderColor;

  //背景颜色
  final Color? backgroundColor;

  //选中的文字颜色
  final Color? selectTextColor;

  //选中的背景色
  final Color? selectedBackgroundColor;

  //选中的边框颜色
  final Color? selectBorderColor;

  final Color? selectedIconColor;

  const HcTag(
      {super.key,
      this.isSelected = false,
      this.disabled = false,
      this.isReverse = false,
      this.padding,
      this.onPressed,
      this.gradient,
      required this.text,
      this.icon,
      this.tagStyle,
      this.themeData,
      this.textColor,
      this.iconColor,
      this.borderColor,
      this.backgroundColor,
      this.selectTextColor,
      this.selectedBackgroundColor,
      this.selectBorderColor,
      this.tagType = HcTagType.normal,
      this.selectedTagType,
      this.selectedIconColor,
      this.maxLines = 1,
      this.borderRadius});

  @override
  Widget build(BuildContext context) {
    //默认的tag外观配置
    HcTagTheme tagTheme = themeData ?? HcTagTheme();
    tagTheme = HcCustomerConfig().allTheme.tagTheme.merge(tagTheme
        .merge(HcTagTheme(padding: padding, borderRadius: borderRadius)));
    //最终的tag颜色配置
    HcTagStyle finalTagStyle = tagStyle ?? HcTagStyle.normal;
    HcTagType getSelectType() {
      if (selectedTagType != null) return selectedTagType!;
      if (tagType == HcTagType.elevated) return HcTagType.outlined;
      if (tagType == HcTagType.normal) return HcTagType.normal;
      return HcTagType.elevated;
    }

    HcTagType finalType = isSelected ? getSelectType() : tagType;
    Color finalBackgroundColor;
    Color finalTextColor;
    Color? finalBorderColor;
    Color finalIconColor;
    switch (finalType) {
      case HcTagType.outlined:
        finalBorderColor = (isSelected ? selectTextColor : textColor) ??
            finalTagStyle.mainColor;
        finalTextColor = (isSelected ? selectTextColor : textColor) ??
            finalTagStyle.mainColor;
        finalBackgroundColor = Colors.transparent;
        finalIconColor =
            (isSelected ? selectedIconColor : iconColor) ?? finalTextColor;
        break;
      case HcTagType.state:
        finalBorderColor = isSelected ? selectBorderColor : borderColor;
        finalTextColor = (isSelected ? selectTextColor : textColor) ??
            finalTagStyle.mainColor;
        finalBackgroundColor =
            (isSelected ? selectedBackgroundColor : backgroundColor) ??
                finalTagStyle.backgroundColor;
        finalIconColor =
            (isSelected ? selectedIconColor : iconColor) ?? finalTextColor;
        break;
      case HcTagType.elevated:
        finalBorderColor = isSelected ? selectBorderColor : borderColor;
        finalBackgroundColor =
            (isSelected ? selectedBackgroundColor : backgroundColor) ??
                finalTagStyle.mainColor;
        finalTextColor = HcColorUtil.needLightColor(finalBackgroundColor)
            ? tagTheme.commonTheme.textInverseColor
            : tagTheme.commonTheme.textBaseColor;
        finalIconColor =
            (isSelected ? selectedIconColor : iconColor) ?? finalTextColor;
        break;
      case HcTagType.normal:
        finalBorderColor = isSelected ? selectBorderColor : borderColor;
        finalBackgroundColor =
            (isSelected ? selectedBackgroundColor : backgroundColor) ??
                HcTagStyle.normal.mainColor;
        finalTextColor = isSelected
            ? finalTagStyle.mainColor
            : HcColorUtil.needLightColor(finalBackgroundColor)
                ? tagTheme.commonTheme.textInverseColor
                : tagTheme.commonTheme.textBaseColor;
        finalIconColor =
            (isSelected ? selectedIconColor : iconColor) ?? finalTextColor;
        break;
    }
    finalBackgroundColor =
        gradient != null ? Colors.transparent : finalBackgroundColor;
    return HcBaseButton(
      elevation: 0,
      onPressed: onPressed,
      disabled: disabled,
      gradient: gradient,
      text: text,
      stateIconReverse: isReverse,
      themeData: HcButtonTheme(
        textColor: finalTextColor,
        maxLines: maxLines,
        textStyle: TextStyle(
            fontSize: tagTheme.fontSize,
            fontWeight: tagTheme.fontWeight,
            height: tagTheme.fontHeight),
        side: finalBorderColor != null
            ? BorderSide(color: finalBorderColor, width: tagTheme.borderWidth)
            : null,
        borderRadius: tagTheme.borderRadius,
        padding: tagTheme.padding,
        miniSize: tagTheme.minSize,
        stateIconSpacing: tagTheme.iconSpacing,
        stateIconColor: finalIconColor,
        stateIconSize: tagTheme.iconSize,
        backgroundColor: finalBackgroundColor,
        disabledTextColor: finalTextColor,
        disabledBackgroundColor: finalBackgroundColor,
      ),
      stateIcon: icon,
    );
  }
}

class HcTagStyle {
  // 主题色
  Color mainColor;

  //背景颜色
  Color backgroundColor;

  HcTagStyle({
    required this.mainColor,
    Color? backgroundColor,
  }) : backgroundColor = backgroundColor ??
            HcColorUtil.mixColor(secondaryColor: mainColor, opacity: 0.2);

  static HcTagStyle normal = HcTagStyle(
    mainColor: HcCustomerConfig().allTheme.commonTheme.textDisableColor,
  );
  static HcTagStyle success = HcTagStyle(
      mainColor: HcCustomerConfig().allTheme.commonTheme.successColor);
  static HcTagStyle warning = HcTagStyle(
      mainColor: HcCustomerConfig().allTheme.commonTheme.warningColor);
  static HcTagStyle error =
      HcTagStyle(mainColor: HcCustomerConfig().allTheme.commonTheme.errorColor);
  static HcTagStyle primary = HcTagStyle(
      mainColor: HcCustomerConfig().allTheme.commonTheme.primaryColor);
  static HcTagStyle notice = HcTagStyle(
      mainColor: HcCustomerConfig().allTheme.commonTheme.noticeColor);
  static HcTagStyle help =
      HcTagStyle(mainColor: HcCustomerConfig().allTheme.commonTheme.helpColor);
}
